Skip to content

Method: IndirectSet(Object, Field, UnitOfWorkImpl, Set)

1: /**
2: * Copyright (C) 2023 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.adapters;
16:
17: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
18:
19: import java.lang.reflect.Field;
20: import java.util.*;
21:
22: public class IndirectSet<E> extends IndirectCollection<Set<E>> implements Set<E> {
23:
24: private final Set<E> internalSet;
25:
26: /**
27: * No-arg constructor to allow clone building.
28: */
29: IndirectSet() {
30: this.internalSet = new HashSet<>();
31: }
32:
33: public IndirectSet(Object owner, Field f, UnitOfWorkImpl uow, Set<E> referencedSet) {
34: super(owner, f, uow);
35: this.internalSet = Objects.requireNonNull(referencedSet);
36: }
37:
38: @Override
39: public int size() {
40: return internalSet.size();
41: }
42:
43: @Override
44: public boolean isEmpty() {
45: return internalSet.isEmpty();
46: }
47:
48: @Override
49: public boolean contains(Object o) {
50: return internalSet.contains(o);
51: }
52:
53: @Override
54: public Iterator<E> iterator() {
55: return new IndirectSetIterator<>(internalSet.iterator());
56: }
57:
58: @Override
59: public Object[] toArray() {
60: return internalSet.toArray();
61: }
62:
63: @Override
64: public <T> T[] toArray(T[] a) {
65: return internalSet.toArray(a);
66: }
67:
68: @Override
69: public boolean add(E e) {
70: boolean res = internalSet.add(e);
71: if (res) {
72: persistChange();
73: }
74: return res;
75: }
76:
77: @Override
78: public boolean remove(Object o) {
79: boolean res = internalSet.remove(o);
80: if (res) {
81: persistChange();
82: }
83: return res;
84: }
85:
86: @Override
87: public boolean containsAll(Collection<?> c) {
88: return internalSet.containsAll(c);
89: }
90:
91: @Override
92:
93: public boolean addAll(Collection<? extends E> c) {
94: boolean res = internalSet.addAll(c);
95: if (res) {
96: persistChange();
97: }
98: return res;
99: }
100:
101: @Override
102: public boolean retainAll(Collection<?> c) {
103: boolean res = internalSet.retainAll(c);
104: if (res) {
105: persistChange();
106: }
107: return res;
108: }
109:
110: @Override
111: public boolean removeAll(Collection<?> c) {
112: boolean res = internalSet.removeAll(c);
113: if (res) {
114: persistChange();
115: }
116: return res;
117: }
118:
119: @Override
120: public void clear() {
121: internalSet.clear();
122: persistChange();
123: }
124:
125: private class IndirectSetIterator<T> implements Iterator<T> {
126:
127: private final Iterator<T> iterator;
128:
129: private IndirectSetIterator(Iterator<T> iterator) {
130: this.iterator = iterator;
131: }
132:
133: @Override
134: public boolean hasNext() {
135: return iterator.hasNext();
136: }
137:
138: @Override
139: public T next() {
140: return iterator.next();
141: }
142:
143: @Override
144: public void remove() {
145: iterator.remove();
146: IndirectSet.this.persistChange();
147: }
148: }
149:
150: @Override
151: public Set<E> unwrap() {
152: return internalSet;
153: }
154:
155: @Override
156: public boolean equals(Object o) {
157: if (o instanceof Set) {
158: if (o instanceof IndirectSet) {
159: return internalSet.equals(((IndirectSet) o).internalSet);
160: }
161: return internalSet.equals(o);
162: }
163: return false;
164: }
165:
166: @Override
167: public int hashCode() {
168: return internalSet.hashCode();
169: }
170:
171: @Override
172: public String toString() {
173: return internalSet.toString();
174: }
175: }